package cn.com.homeyoda.aimate.aihealth.ui.message;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import androidx.annotation.NonNull;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.ArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.BannerBean;
import cn.com.homeyoda.aimate.aihealth.bean.BaseArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageListBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTitleBean;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTypeListBean;
import cn.com.homeyoda.aimate.aihealth.bean.ReMenBean;
import cn.com.homeyoda.aimate.aihealth.bean.ZuiXinBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import com.wzq.mvvmsmart.event.SingleLiveEvent;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.http.ResponseThrowable;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.ui.videoinspect.VideoInspectModel;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created By liuxinhua
 * Created Time 2025/2/27
 */
public class MessageModel extends BaseViewModel<Repository> {

    public StateLiveData<MessageTypeListBean> stateLiveData;
    public StateLiveData<MessageListBean> messageData;

    public StateLiveData<BaseResponse2> readMsgData;

    public MessageModel(@NonNull Application application, Repository model) {
        super(application, model);
        stateLiveData = new StateLiveData<>();
        stateLiveData.setValue(new MessageTypeListBean());
        messageData = new StateLiveData<>();
        messageData.setValue(new MessageListBean());
        readMsgData = new StateLiveData<>();
        readMsgData.setValue(new BaseResponse2());


    }

    public void messageTitleData(){

        model.getMessageTypeList()
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MessageModel.this)
                .subscribe(new Observer<MessageTypeListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stateLiveData.postLoading();
                    }

                    @Override
                    public void onNext(MessageTypeListBean messageTypeListBean) {
                        if(messageTypeListBean.getCode() == 200){
                            MessageTypeListBean.DataBean dataBean = new MessageTypeListBean.DataBean();
                            dataBean.setTypeName("全部");
                            dataBean.setTypeValue("0");
                            messageTypeListBean.getData().add(0,dataBean);
//                            stateLiveData.postSuccess();
//                            stateLiveData.setValue(messageTypeListBean);
                            stateLiveData.postValueAndSuccess(messageTypeListBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        stateLiveData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        stateLiveData.postIdle();

                    }
                });
    }

    public void messageData(){
        Log.i("huage","messageData====userId==="+model.getUserId());
        model.getMessageList(model.getUserId(), "","")
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MessageModel.this)
                .subscribe(new Observer<MessageListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        messageData.postLoading();
                    }

                    @Override
                    public void onNext(MessageListBean messageListBean) {
                        if(messageListBean.getCode() == 200){
//                            messageData.postSuccess();
//                            messageData.setValue(messageListBean);
                            messageData.postValueAndSuccess(messageListBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        messageData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        messageData.postIdle();

                    }
                });
    }

    public void readMessage(String id){
        model.readMsg(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MessageModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        readMsgData.postLoading();
                    }

                    @Override
                    public void onNext(BaseResponse2 baseResponse2) {
//                        readMsgData.postSuccess();
//                        readMsgData.setValue(baseResponse2);
                        readMsgData.postValueAndSuccess(baseResponse2);

                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        //关闭对话框
                        readMsgData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        //请求刷新完成收回
                        readMsgData.postIdle();

                    }
                });
    }

}
